Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding lib support to crossgen #12

Merged
merged 3 commits into from
Sep 17, 2018
Merged

Conversation

spacekookie
Copy link
Contributor

@spacekookie spacekookie commented Sep 13, 2018

Choose one: This a πŸ™‹ feature

So, this is just quickly something I threw together on the plane – looking for
some feedback!

The CLI now has a --lib flag which is taken into account when creating
a template. Template::new still exists, but is deprecated and points
to Template::gen_bin (just in case this shouldn't be a breaking change).

Actually generating the templates I'm not 100% sure what differences
might be wanted/required for bin vs lib. So I just copied the templates
directory for now.

Also the write block is quite ugly now. Considering that this approach
generally isn't very modular, I'm wondering if it wouldn't be possible
to have a folder with travis "snippets" that are then assembled according
to some user config (e.g. I want android but not wasm, but it's also a binary?)

Again...WIP – happy to change stuff!

Cheers!

Checklist

  • tests pass
  • tests and/or benchmarks are included
  • documentation is changed or added

Context

This PR addresses issue #11

Semver Changes

This is a non-breaking change (for now)


$ZIP = "$SRC_DIR\$($Env:CRATE_NAME)-$($Env:APPVEYOR_REPO_TAG_NAME)-$($Env:TARGET).zip"

Copy-Item "$SRC_DIR\target\$($Env:TARGET)\release\$PKG_NAME.exe" '.\'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On windows we might want to have this as $PKG_NAME.dll and copy it to .\win-$target-$PKG_NAME.dll as I think GitHub don't support folders

New-Item -Type Directory -Name $STAGE
Set-Location $STAGE

$ZIP = "$SRC_DIR\$($Env:CRATE_NAME)-$($Env:APPVEYOR_REPO_TAG_NAME)-$($Env:TARGET).zip"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When thinking of the distribution aspect of the lib on github, I think it would be more helpful to have the bare-ddl not zipped.

In my mind, when building a library on another language, it could be possible to instrument the build system to download all the .so and .dll into a folder to distribute the package with it, and having it not-zipped would make the build system much simpler.

What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the zip archive really only makes a big difference if you have a library that produces more than one artefact (which can easily be the case). Maybe it should be something that is configurable somehow?

test -f Cargo.lock || cargo generate-lockfile

cross rustc --bin $PKG_NAME --target $TARGET --release -- -C lto
cp target/$TARGET/release/$PKG_NAME $stage/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think cargo generates libs in the form of ${PKG_NAME}lib.so

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it was lib${PKG_NAME}.so unless you manually set the lib.name field?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, you are right!


# Install test dependencies
rustup component add rustfmt-preview
rustup component add clippy-preview
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if we also install bindgen and setup a build stage responsible to build the .h headers and publish on Github?

Copy link
Owner

@yoshuawuyts yoshuawuyts Sep 14, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few quick notes here:

  • we would need cbindgen, not bindgen here (e.g. wrap rust code to be consumed from C, not the other way around)
  • here's an example of how to do that
  • in the example it stands out to me that a cbindgen.toml file is included. Should we generate that as well, or leave that up to the user? It looks like it might not be required.
  • building with cbindgen can either be done from the command line or from build.rs, I'm not sure which one is preferable. But we should probably pick one.

@bltavares
Copy link
Contributor

That is neat! I would be able to have some time next week to test drive this on a sample project.

One thing I've considered when reading the Issue would be to provide some feedback on the Cargo.toml setup, or even modify it automatically to add the crate-type = ["rlib", "dylib", ...] if not there yet. This could come later as well, but it would be a nice touch to me.

src/cli.rs Outdated
@@ -18,6 +18,9 @@ pub struct Cli {
/// Project name. Defaults to target directory name
#[structopt(short = "n", long = "name")]
name: Option<String>,
/// Specify if a library template should be generated
#[structopt(short = "l", long = "lib")]
lib: Option<bool>,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe lib: bool should work well enough. It defaults to false, and is only set to true if you pass --lib to the executable. This should make it slightly easier to access in the code.

src/main.rs Outdated
@@ -25,6 +25,7 @@ fn main() -> Result<(), ExitFailure> {

let dir = args.dir()?;
let name = args.name()?;
let lib = args.lib().unwrap_or(false);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If args.lib() returns a bool, we can remove .unwrap_or(false) from this line.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I opted for this so that it's more clear in main.rs what the default vaulue is (instead of hiding it in cli.rs. We can of course change this

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah okay, gotcha. Perhaps a better way to be explicit about the default state is to add default_value=false to the structopt attribute. I was a bit confused why we had 3 states for a boolean value (e.g. not passed at all, passed as true, passed as false?) instead of defaulting to a "is this option enabled? yes / no"

Copy link
Owner

@yoshuawuyts yoshuawuyts left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the general idea a lot; think this would be fine to merge as-is. Commented with a few small questions / quality-of-life improvements. Hope they're alright. Thanks for doing this! πŸŽ‰

@bltavares
Copy link
Contributor

Oh, I've just remembered that you can pass to cargo which type of lib it should build. I guess that my idea to fiddle with Cargo.toml would not be necessary at all.

crossgen --lib --lib-type=dynamic could introduce a new interpolated variable on the build when calling cargo. The nice thing is that it would be easy to switch to a static lib by regenerating, or by changing the definitions in the build matrix.

@spacekookie
Copy link
Contributor Author

spacekookie commented Sep 14, 2018

cc/ @yoshuawuyts So I changed the CLI handling to be more direct (no Option<bool>).

I think the question of how the library template files should work is not an easy one to solve. And also, maybe we can come up with a better and more modular way of storing/ generating them – aka just having a few code snippets that can be assembled into larger scripts that get generated.

But that's a bit beyond what this PR was meant to be.

Also: not sure why the CI fails? Any advice on that?

@spacekookie spacekookie changed the title [WIP] Adding lib support to crossgen Adding lib support to crossgen Sep 14, 2018
@yoshuawuyts
Copy link
Owner

yoshuawuyts commented Sep 14, 2018

@spacekookie it looks like cargo fmt wasn't run, which is the root cause of the issues. Also DCO bot is failing because commits aren't signed off with git commit -s (but also strongly considering removing DCO bot for my own work btw).

Screenshot

This is the cargo fmt diff:

2018-09-14-183449_1920x1080

@yoshuawuyts
Copy link
Owner

I think the question of how the library template files should work is not an easy one to solve. And also, maybe we can come up with a better and more modular way of storing/ generating them – aka just having a few code snippets that can be assembled into larger scripts that get generated.

But that's a bit beyond what this PR was meant to be.

I agree that it'd be great to figure out something for this, but agree it's best to do so somewhere separate. Perhaps we could draft an issue for this where we can discuss the options?

@spacekookie
Copy link
Contributor Author

spacekookie commented Sep 15, 2018

@yoshuawuyts I'll try re-running rustfmt then, although I thought I had already done that. Is there some specific version I need to use? Also…I sign my commits with gpg but don't really use -s normally. I guess I'll just do that for now.

Also, I just opened #13 to discuss some of these issues πŸŽ‰

So, this is just quickly something I threw together on the plane – looking for
some feedback!

The CLI now has a `--lib` flag which is taken into account when creating
a template. `Template::new` still exists, but is deprecated and points
to `Template::gen_bin` (just in case this shouldn't be a breaking change).

Actually generating the templates I'm not 100% sure what differences
might be wanted/required for bin vs lib. So I just copied the templates
directory for now.

Also the write block is quite ugly now. Considering that this approach
generally isn't very modular, I'm wondering if it wouldn't be possible
to have a folder with travis "snippets" that are then assembled according
to some user config (e.g. I want android but not wasm, but it's also a binary?)

Again...WIP – happy to change stuff!

Cheers!

Signed-off-by: Katharina Fey <[email protected]>
Signed-off-by: Katharina Fey <[email protected]>
@yoshuawuyts
Copy link
Owner

Got a build to pass on a re-run (which means travis failures are just a clippy problem) https://travis-ci.com/yoshuawuyts/crossgen/jobs/145930817, so think this is good to merge for now! ✨

@yoshuawuyts yoshuawuyts merged commit 6879674 into yoshuawuyts:master Sep 17, 2018
@yoshuawuyts
Copy link
Owner

v0.5.0 πŸŽ‰

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants